#define _CRT_SECURE_NO_WARNINGS 1
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x)
    {
        if (head == nullptr || head->next == nullptr)
        {
            return head;
        }
        ListNode* cur1 = head, * cur2 = head;
        while (cur1 && cur2)
        {
            while (cur1 && cur1->val < x)
            {
                cur1 = cur1->next;
            }
            cur2 = cur1;

            while (cur2 && cur2->val >= x)
            {
                cur2 = cur2->next;
            }
            if (!cur1 || !cur2)
            {
                break;
            }
            int tmp = cur1->val;
            cur1->val = cur2->val;
            cur2->val = tmp;
        }
        return head;
    }
};